package com.yzk.auction.dao;

import com.yzk.auction.entity.Users;
import com.yzk.auction.utils.JDBCUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.List;

public class UsersDao {
    private DataSource dataSource = JDBCUtils.getDataSource();
    private Users users = null;
    public Users getone(String user_id){
        QueryRunner queryRunner = new QueryRunner(dataSource);
        try {
            Users user = queryRunner.query("SELECT * FROM tb_users WHERE User_id = ?",new BeanHandler<Users>(Users.class),user_id);
            return user;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return null;
        }
    }
    public List<Users> getList(){
        QueryRunner queryRunner = new QueryRunner(dataSource);
        try {
            List<Users> usersList = queryRunner.query("SELECT * FROM tb_users", new BeanListHandler<Users>(Users.class));
            //System.out.println(usersList);
            return usersList;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }
    public int udelete(String user_id){
        QueryRunner queryRunner = new QueryRunner(dataSource);
        try {
            int result = queryRunner.update("DELETE FROM tb_users WHERE user_id=?", user_id);
            return result;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return 0;
        }
    }
    public int uupdate(String user_id, String user_name, String user_pwd, String user_sex, String user_salary, String user_tel, String user_addr) {
        QueryRunner queryRunner = new QueryRunner(dataSource);
        try {
            int result=queryRunner.update("update tb_users set user_name=?,user_pwd=?,user_sex=?,user_salary=?,user_tel=?,user_addr=? where user_id=?",
                    user_name,
                    user_pwd,
                    user_sex,
                    user_salary,
                    user_tel,
                    user_addr,
                    user_id);
            return result;
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }
}
